package com.example.springback.service;

import com.example.springback.domain.Employee;
import com.example.springback.domain.Notice;
import com.example.springback.exception.CustomException;
import com.example.springback.mapper.NoticeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {
    @Resource
    NoticeMapper noticeMapper;

    public void insert(Notice notice) {
        Notice notice1=noticeMapper.selectByDateAndTitle(notice.getPublishTime(),notice.getTitle());
        if(notice1!=null){
            throw new CustomException("500","不能在统一发布日期发布相同标题的公告");
        }
        noticeMapper.insert(notice);
    }

    public PageInfo<Notice> selectPage(Notice notice, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Notice> notices=noticeMapper.selectAll(notice);
        return PageInfo.of(notices);
    }

    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    public void delete(Integer id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Notice> notices) {
        for (Notice notice:notices){
            noticeMapper.deleteByPrimaryKey(notice.getId());
        }
    }

    public List<Notice> selectTen(Notice notice) {
        return noticeMapper.selectTen(notice.getNoticeType());
    }

    public Notice selectById(Integer id) {
        return noticeMapper.selectByPrimaryKey(id);
    }
}
